package com.zhutengwei.android.zuji.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zhutengwei.android.zuji.model.UserModel;
import com.zhutengwei.android.zuji.model.ZujiModel;

import java.util.ArrayList;
import java.util.List;

/**拼写SQL语句
 * Created by Administrator on 2016/12/18.
 */
public class TableDao {

    private SQLiteDatabase mDatabase;

    public TableDao(Context context){
        mDatabase = DbOpenHelper
                .getInstance(context, "zuji.db")
                .getWritableDatabase();
    }

    /**
     * 插入一个用户
     * @param user
     */
    public void addUser(UserModel user){
        ContentValues values = getUserContentValues(user);
        mDatabase.insert(UserTable.TABLE_NAME, null, values);
    }

    /**
     * 插入一条足迹
     * @param zuji
     */
    public void addZuji(ZujiModel zuji){
        ContentValues values = getZujiContentValues(zuji);
        mDatabase.insert(ZujiTable.TABLE_NAME, null, values);
    }

    /**
     * 查询用户
     * @param whereClause
     * @param whereArgs
     * @return
     */
    private UserCursorWrapper queryUsers(String whereClause, String[] whereArgs){
        Cursor cursor = mDatabase.query(
                UserTable.TABLE_NAME,
                null,
                whereClause,
                whereArgs,
                null,
                null,
                null
        );
        return new UserCursorWrapper(cursor);
    }

    /**
     * 查询zuji
     * @param whereClause
     * @param whereArgs
     * @return
     */
    private ZujiCursorWrapper queryZujis(String whereClause, String[] whereArgs){
        Cursor cursor = mDatabase.query(
                ZujiTable.TABLE_NAME,
                null,
                whereClause,
                whereArgs,
                null,
                null,
                ZujiTable.COLUMN_ID + " DESC"
        );
        return new ZujiCursorWrapper(cursor);
    }
    /**
     * 获取数据库用户表字段
     * @param user
     * @return
     */
    public static ContentValues getUserContentValues(UserModel user){
        ContentValues values = new ContentValues();
        values.put(UserTable.COLUMN_NICKNAME, user.getNickname());
        values.put(UserTable.COLUMN_ADDRESS, user.getAddress());
        values.put(UserTable.COLUMN_EMAIL, user.getEmail());
        values.put(UserTable.COLUMN_PHONENUM, user.getPhonenum());
        values.put(UserTable.COLUMN_USERPASS, user.getUserpass());
        values.put(UserTable.COLUMN_AVATAR, user.getAvatar());
        values.put(UserTable.COLUMN_SIGNATURE, user.getSignature());
        values.put(UserTable.COLUMN_REGISTERDATE, user.getRegisterdate());

        return values;
    }

    /**
     * 获取数据库zuji表字段
     * @param zuji
     * @return
     */
    public static ContentValues getZujiContentValues(ZujiModel zuji){
        ContentValues values = new ContentValues();
        values.put(ZujiTable.COLUMN_EMAIL, zuji.getEmail());
        values.put(ZujiTable.COLUMN_CATEGORY, zuji.getCategory());
        values.put(ZujiTable.COLUMN_TITLE, zuji.getTitle());
        values.put(ZujiTable.COLUMN_CONTENT, zuji.getContent());
        values.put(ZujiTable.COLUMN_LATITUDE, zuji.getLatitude());
        values.put(ZujiTable.COLUMN_LONGITUDE, zuji.getLongitude());
        values.put(ZujiTable.COLUMN_IMGDATA, zuji.getImgdata());
        values.put(ZujiTable.COLUMN_DATE, zuji.getDate());

        return values;
    }

    /**
     * 通过用户邮件名获取UserModel
     * @param email
     * @return
     */
    public UserModel getUserByEmail(String email){
        UserCursorWrapper cursor = queryUsers(
                UserTable.COLUMN_EMAIL + " = ?",
                new String[] {email}
        );
        try{
            if(cursor.getCount() == 0){
                return null;
            }
            cursor.moveToFirst();
            return cursor.getUser();
        } finally {
            cursor.close();
        }

    }

    /**
     * 通过用户邮件名获取ZujiModel
     * @param email
     * @return
     */
    public List<ZujiModel> getZujiByEmail(String email){
        List<ZujiModel> list = new ArrayList<>();
        ZujiCursorWrapper cursor = queryZujis(
                ZujiTable.COLUMN_EMAIL + " = ?",
                new String[] {email}
        );

        try{
            if(cursor.getCount() == 0){
                return null;
            }
            if(cursor.moveToFirst() ){
                do{
                    list.add(cursor.getZuji());
                }while(cursor.moveToNext());
            }
            return list;
        } finally {
            cursor.close();
        }

    }
    /**
     * 通过Date获取ZujiModel
     * @param date
     * @return
     */
    public ZujiModel getZujiByDate(String date){
        ZujiCursorWrapper cursor = queryZujis(
                ZujiTable.COLUMN_DATE + " = ?",
                new String[] {String.valueOf(date)}
        );
        try{
            if(cursor.getCount() == 0){
                return null;
            }
            cursor.moveToFirst();
            return cursor.getZuji();
        } finally {
            cursor.close();
        }

    }

    public int deleteZujiByDate(String date){
        return mDatabase.delete(
                ZujiTable.TABLE_NAME,
                ZujiTable.COLUMN_DATE + "=?",
                new String[]{date}
        );
    }

    public void close(){
        mDatabase.close();
    }


}
